package com.ruoyi.system.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.News;
import com.ruoyi.system.mapper.NewsMapper;
import com.ruoyi.system.service.NewsService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    private final NewsMapper newsMapper;

    public NewsServiceImpl(NewsMapper newsMapper) {
        this.newsMapper = newsMapper;
    }

    @Override
    public PageInfo<News> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(newsMapper.selectAll());
    }

    @Override
    public News findById(Integer id) {
        return newsMapper.selectById(id);
    }


    @Override
    public boolean save(News news) {
        if (newsMapper.checkTitleExists(news.getTitle()) > 0) {
            return false;
        }
        return newsMapper.insert(news) > 0;
    }

    @Override
    public boolean update(News news) {
        if (newsMapper.checkTitleExists(news.getTitle()) > 0 &&
                !newsMapper.selectById(news.getId()).getTitle().equals(news.getTitle())) {
            return false;
        }
        return newsMapper.update(news) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return newsMapper.delete(id) > 0;
    }

    @Override
    public boolean checkTitle(String title, Integer excludeId) {
        int count = newsMapper.checkTitleExists(title);
        if (excludeId != null) {
            News original = newsMapper.selectById(excludeId);
            if (original != null && original.getTitle().equals(title)) {
                return true;
            }
        }
        return count == 0;
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public News selectNewsById(Long id)
    {
        return newsMapper.selectNewsById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param news 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<News> selectNewsList(News news)
    {
        return newsMapper.selectNewsList(news);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param news 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertNews(News news)
    {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        news.setCreateTime(formatter.format(localDateTime));
        return newsMapper.insertNews(news);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param news 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateNews(News news)
    {
        return newsMapper.updateNews(news);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNewsByIds(Long[] ids)
    {
        return newsMapper.deleteNewsByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNewsById(Long id) {
        return newsMapper.deleteNewsById(id);
    }
}
